import { ResponseResult } from "@/entity/ResponseResult";
import UmsMember from "@/entity/UmsMember";
import UmsMemberChangePasswordParam from "@/entity/UmsMemberChangePasswordParam";
import { UmsMemberCreationParam } from "@/entity/UmsMemberCreationParam";
import { AxiosError, AxiosResponse } from "axios";
import httpClient from "./http-client";
import { db_members } from "@/database/member";
import store, { TOKEN_USER_ID } from "@/store";

export class MemberService {
    baseUrl = "/api/v1.0/portal/member";

    public async register(param: UmsMemberCreationParam) {
        if (param.verifyCode != "7xv6") {
            throw new AxiosError("验证码错误");
        }
        if (param.confirmPassword != param.password) {
            throw new AxiosError("两次密码输入不一致");
        }
        const member: UmsMember = {
            id: Math.max(...db_members.map(it => it.id)) + 1,
            username: param.username,
            password: param.password,
            nickname: null,
            phone: null,
            avatarUrl: "/mock-data/file-storage/avatar/default.png",
            "createdTime": "2023-06-10 11:16:44",
            "lastModifiedTime": "2023-06-10 11:16:44",
        };
        db_members.push(member);

        return Promise.resolve({
            "code": 200,
            "message": "操作成功",
            "data": member.id
        });

        const body = {
            ...param
        };
        return httpClient.post(`${this.baseUrl}/register`, body).then((res: AxiosResponse<ResponseResult<number>>) => {
            if (res.data.data) {
                return Promise.resolve(res.data.data);
            } else {
                throw "no data";
            }
        });
    }

    public async update(param: UmsMember) {
        if (param.id == null) {
            throw new AxiosError("id不可为空");
        }
        const member = db_members.find(it => it.id == param.id);
        if (member == null) {
            throw new AxiosError("未找到用户");
        }

        member.avatarUrl = param.avatarUrl;
        member.nickname = param.nickname
        member.phone = param.phone

        return Promise.resolve({
            "code": 200,
            "message": "操作成功",
            "data": member.id
        });
        const body = {
            ...param
        };
        return httpClient.put(`${this.baseUrl}`, body).then((res: AxiosResponse<ResponseResult<number>>) => {
            if (res.data.data) {
                const id = res.data.data;
                return Promise.resolve(id);
            } else {
                throw "no data";
            }
        });
    }

    public async changePassword(param: UmsMemberChangePasswordParam) {
        if (param.verifyCode != "7xv6") {
            throw new AxiosError("验证码错误");
        }
        const userId = Number(store.getters[TOKEN_USER_ID]);
        if (param.confirmPwd != param.newPwd) {
            throw new AxiosError("两次密码输入不一致");
        }

        const member = db_members.find(it => it.id == userId);
        if (member == null) {
            throw new AxiosError("未找到用户");
        }
        if (member.password != param.oldPwd) {
            throw new AxiosError("旧密码错误");
        }
        member.password = param.newPwd;
        return Promise.resolve({
            "code": 200,
            "message": "操作成功",
            "data": member.id
        });
        const body = {
            ...param
        };
        return httpClient.put(`${this.baseUrl}/change-password`, body).then((res: AxiosResponse<ResponseResult<number>>) => {
            if (res.data.data) {
                const id = res.data.data;
                return Promise.resolve(id);
            } else {
                throw "no data";
            }
        });
    }
}

const instance = new MemberService();
export default instance;